// pages/home/home.js
import list from '../../datas/home';

const SCROLL = 1000
Page({
  /**
   * 页面的初始数据
   */
  data: {
     banners: [], //首页轮播图片
     recommends: [], //首页推荐
     tabs: ['流行', '新款', '精选'], 
     goods: {
       'pop': {page: 0, list:[]},
       'news': {page: 0, list:[]},
       'sell': {page: 0, list:[]}
     },
     currentType:'pop',
     type: ['pop', 'news', 'sell'],
     showBackTop: false,
     isTabFixed: false,
     tabControlTop: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this._getBanners();
    this._getRecommends();
    this._getGoods('pop');
    this._getGoods('news');
    this._getGoods('sell');
  },
  /*-----------------获取数据函数-------**/
  _getBanners() {
    const banners = [
      {
        index: 1,
        image: "/assets/home/swiper/1.jpg"
      },
      {
        index: 2,
        image: "/assets/home/swiper/2.jpg"
      },
      {
        index: 3,
        image: "/assets/home/swiper/3.jpg"
      },
      {
        index: 4,
        image: "/assets/home/swiper/4.jpg"
      }
    ];
    this.setData({
      banners
    });
  },
  _getRecommends() {
    const recommends = [
      {
        image: '/assets/home/recommonds/1.jpg',
        title: '十点抢卷',
      },
      {
        image: '/assets/home/recommonds/2.jpg',
        title: '好物特卖',
      },
      {
        image: '/assets/home/recommonds/3.jpg',
        title: '内购福利',
      },
      {
        image: '/assets/home/recommonds/4.jpg',
        title: '初秋上新',
      }
    ];
     this.setData({
       recommends
     });
  },
  _getGoods(currentType) {
      const { goods } = this.data;
      const currentPage = goods[currentType].page;
      const newList = list[currentType].slice(currentPage * 30, (currentPage + 1) * 30);
      const oldList = goods[currentType].list;
      oldList.push(...newList);
      const typeKey = `goods.${currentType}.list`;
      const pageKey = `goods.${currentType}.page`; 
      this.setData({
        [typeKey]: oldList,
        [pageKey]: currentPage + 1
      });
  },

  /*-----------------页面操作函数-------**/
  handldTabClick(event) {
    const index = event.detail.index;
    this.setData({
      currentType: this.data.type[index]
    });
  },  
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    setTimeout(()=> {
      wx.createSelectorQuery().select('#tab-control').boundingClientRect(rect => {
        this.setData({
          tabControlTop: rect.top
        })
      }).exec();
    },1000)
     
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     this._getGoods(this.data.currentType);
  },

  /**
   * 监听页面滚动
   */
  onPageScroll: function (options) {
    const backTopFlag = options.scrollTop > SCROLL;
    if(backTopFlag !== this.data.showBackTop) {
      this.setData({
        showBackTop: backTopFlag
      })
    }
    const tabControlFlag = options.scrollTop >= this.data.tabControlTop;
    if(tabControlFlag !== this.data.isTabFixed) {
      this.setData({
        isTabFixed: tabControlFlag
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})